<!-- @Author: Yu_Bo -->
<template>
	<view class="booksku">
		<u-popup v-model="show" mode="bottom" :safe-area-inset-bottom="safearea" :closeable="true" border-radius="40" :mask-close-able="false">
			<view class="main">
				<view class="main_title line_one">
					{{name}}
				</view>
				<view class="main_sku">
					<view class="sku_list" :class="listIndex==index?'sku_list_active':''" v-for="(item,index) in list" :key="index" @click="changBtn(item,index)">
						<view class="list_left">
							<view class="txt">￥</view>
							<view class="money">{{item.price}}</view>
							<view class="tip color1" v-if="item.type == 0">二手{{item.discount}}折</view>
							<view class="tip color2" v-if="item.type == 1">新书{{item.discount}}折</view>
						</view>
						<view class="list_right" :class="item.num?'':'color3'" v-if="type==1">
							{{item.label}}{{item.num?'':'(暂时无货)'}}
						</view>
						<view class="list_right" :class="item.num*1>=sku_nun*1?'':'color3'" v-if="type==2">
							{{item.label}}{{item.num*1>=sku_nun*1?'':'(库存不足)'}}
						</view>
					</view>
				</view>
				<view class="main_btn" :class="safearea?'':'main_btn_bot'">
					<view class="btn_txt color_right page_button" v-if="type==1" @click="addBook">
						<u-loading v-if="loading" mode="flower" color="#ffffff"></u-loading>
						加入书包
					</view>
					<view class="btn_txt color_right page_button" v-if="type==2" @click="submitBtn">
						<u-loading v-if="loading" mode="flower" color="#ffffff"></u-loading>
						确定
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		components: {},
		props: {
			type: {
				type: Number,
				default: 1
			},
			safearea: {
				type: Boolean,
				default: true
			},
		},
		data() {
			return{
				show: false,
				name: "",
				list: [],
				listIndex: null,
				loading: false,
				// 
				cart_id: null,
				sku_index: null,
				sku_nun: null,
			}
		},
		computed: {},
		mounted() {},
		methods: {
			// 打开
			openBtn(name,arr) {
				this.name = name
				this.list = arr
				this.show = true
			},
			// 打开
			openSkuBtn(name,index,num,cart_id,arr) {
				this.name = name
				this.sku_index = index
				this.listIndex = index
				this.sku_nun = num
				this.cart_id = cart_id
				this.list = arr
				this.show = true
			},
			// 关闭
			closeBtn() {
				this.show = false
			},
			// 选择
			changBtn(item,index) {
				if(this.type == 1) {
					if(item.num) {
						this.listIndex = index
					} else {
						this.$tips.showToast('暂无库存请选择其他规格')
					}
				}
				if(this.type == 2) {
					if(item.num*1 >= this.sku_nun*1) {
						this.listIndex = index
					} else {
						this.$tips.showToast('库存不足请选择其他规格')
					}
				}
			},
			// 加入书包
			addBook() {
				var that = this
				if(that.listIndex == null) {
					that.$tips.showToast('请选择类型')
					return
				}
				if(that.loading) return
				that.loading = true
				var params = {
					goods_id: that.list[that.listIndex].id,
					condition: that.list[that.listIndex].type,
					goods_num: 1,
				}
				that.$httpApi.cartAdd(params).then((res) => {
					that.loading = false
					if (res.code == 1) {
						that.$tips.showToast(res.msg)
						that.show = false
					}
				})
			},
			// 确定
			submitBtn() {
				var that = this
				if(this.sku_index == this.listIndex) {
					that.show = false
					return
				}
				if(that.loading) return
				that.loading = true
				var params = {
					cart_id: that.cart_id,
					condition: that.listIndex,
				}
				that.$httpApi.specChoice(params).then((res) => {
					that.loading = false
					if (res.code == 1) {
						that.$tips.showToast(res.msg)
						that.show = false
						this.$emit("sub",this.listIndex)
					}
				})
			},
		},
	}
</script>

<style lang='scss' scoped>
	.booksku {
		.main {
			width: 100%;
			
			.main_title {
				width: 100%;
				line-height: 100rpx;
				text-align: center;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #222222;
				border-bottom: 1rpx solid #F6F6F6;
			}
			
			.main_sku {
				width: 100%;
				padding: 0 30rpx;
				
				.sku_list {
					width: 100%;
					height: 112rpx;
					margin-top: 30rpx;
					padding: 0 20rpx;
					background: #F6F6F6;
					border-radius: 12rpx;
					border: 2rpx solid #F6F6F6;
					display: flex;
					align-items: center;
					justify-content: space-between;
					
					.list_left {
						display: flex;
						align-items: center;
						
						.txt {
							padding-top: 5rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 24rpx;
							color: #5A68FF;
						}
						
						.money {
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 36rpx;
							color: #5A68FF;
						}
						
						.tip {
							margin-left: 10rpx;
							padding: 5rpx 10rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							background: #fff;
							border-radius: 10rpx;
						}
						
						.color1 {
							color: #57CB82;
							border: 2rpx solid #57CB82;
						}
						
						.color2 {
							color: #FF9400;
							border: 2rpx solid #FF9400;
						}
					}
					
					.list_right {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #222222;
					}
					
					.color3 {
						color: #999999;
					}
				}
				.sku_list_active {
					border: 2rpx solid #5A68FF;
					background: #E9EBFF;
				}
			}
			
			.main_btn {
				width: 100%;
				padding: 40rpx 30rpx 0;
				
				.btn_txt {
					width: 100%;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 30rpx;
					border-radius: 40rpx;
				}
			}
			
			.main_btn_bot {
				padding-bottom: 40rpx;
			}
		}
	}
</style>